package org.abacus.platform.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidatorUtil {
	public static final Pattern IPV4 = Pattern
			.compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
	public static final Pattern DECIMAL = Pattern.compile("^[-+]?[0-9]*\\.[0-9]+$");

	public static final Pattern INTEGER = Pattern.compile("^[-+]?[0-9]+$");

	public static final Pattern NUMBER = Pattern.compile("^[-+]?[0-9]*\\.?[0-9]+$");

	public static final Pattern EMAIL = Pattern.compile("^[\\w\\.-]+@([\\w\\-]+\\.)+[a-z]{2,4}$");

	public static final Pattern SSN = Pattern.compile("^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$");

	public static final Pattern PHONE_NUMBER = Pattern.compile("^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$");

	/**
	 * Validate ip address with regular expression
	 *
	 * @param ip
	 *            ip address for validation
	 * @return true valid ip address, false invalid ip address
	 */
	public static final boolean isIPV4(final String ip) {
		Matcher matcher = IPV4.matcher(ip);
		return matcher.matches();
	}

	/**
	 * Validate decimal with regular expression
	 *
	 * @param decimal
	 *            decimal for validation
	 * @return true valid decimal, false invalid decimal
	 */
	public static final boolean isDecimal(final String decimal) {
		Matcher matcher = DECIMAL.matcher(decimal);
		return matcher.matches();
	}

	/**
	 * Validate integer with regular expression
	 *
	 * @param integer
	 *            integer for validation
	 * @return true valid integer, false invalid integer
	 */
	public static final boolean isInteger(final String integer) {
		Matcher matcher = INTEGER.matcher(integer);
		return matcher.matches();
	}

	/**
	 * Validate number with regular expression
	 *
	 * @param number
	 *            number for validation
	 * @return true valid number, false invalid number
	 */
	public static final boolean isNumber(final String number) {
		Matcher matcher = NUMBER.matcher(number);
		return matcher.matches();
	}

	/**
	 * Validate email with regular expression
	 *
	 * @param email
	 *            email for validation
	 * @return true valid email, false invalid email
	 */
	public static final boolean isEmail(final String email) {
		Matcher matcher = EMAIL.matcher(email);
		return matcher.matches();
	}

	/**
	 * Validate ssn with regular expression
	 *
	 * @param ssn
	 *            ssn for validation
	 * @return true valid ssn, false invalid ssn
	 */
	public static final boolean isSSN(final String ssn) {
		Matcher matcher = SSN.matcher(ssn);
		return matcher.matches();
	}

	/**
	 * Validate phoneNumber with regular expression
	 *
	 * @param phoneNumber
	 *            phoneNumber for validation
	 * @return true valid phoneNumber, false invalid phoneNumber
	 */
	public static final boolean isPhoneNumber(final String phoneNumber) {
		Matcher matcher = PHONE_NUMBER.matcher(phoneNumber);
		return matcher.matches();
	}
}
